/**
 * 
 */
package neptune.web.client.commands;

import neptune.web.client.controller.UserDetailsDialogController;
import neptune.web.client.model.SerializableUser;
import neptune.web.client.services.user.UserService;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.components.dialogs.UserDetailsDialog;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class OpenUserDetailsCommand implements Command {

	private String username;

	public OpenUserDetailsCommand(String username) {
		this.username = username;
	}

	public void execute() {
		final UserDetailsDialog dialog = new UserDetailsDialog();
		String[] values = new String[]{username, "loading...","loading...","loading..."};
		dialog.setPropertyValues(values);
		dialog.setListener(new UserDetailsDialogController());
		
		UserService.Util.getInstance().getUser(username, new AsyncCallback<SerializableUser>() {

			public void onSuccess(SerializableUser usr) {
				String[] props = new String[]{username,usr.getPassword(),usr.getRole(),usr.getEmail()};
				dialog.setPropertyValues(props);
//				Per indicare che con questi dati impostati, la dialog e' nella situazione di dati invariati
				dialog.setModified(false);
			}

			public void onFailure(Throwable arg0) {
				ConsoleManager.addConsoleMessage("Impossible to retrieve "
						+ username + "'s information: " + arg0.getMessage(),
						true);
			}

		});
		
		dialog.center();
	}

}
